﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

using MicroCenter.Models;
using MicroCenter.Services;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic.CompilerServices;

namespace MicroCenter.Controllers
{
    [Auth]
    [Route("api/[controller]")]
    [ApiController]
    public class FileController : ControllerBase
    {
        public readonly FileService fileService;

        /// <summary>
        /// 查看服务文件夹
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <param name="version">版本号</param>
        /// <param name="path"></param>
        /// <param name="isParent"></param>
        /// <returns></returns>
        [Get("lookFiles/{serviceName}/{version}")]
        public List<FileModel> LookFiles(string serviceName, string version, string path)
        {
            return fileService.GetDirectoryAndFiles(serviceName, version, path);
        }

        [Get("deleteFile/{serviceName}/{version}")]
        public void DeleteFile(string serviceName, string version, string fullName) => fileService.DeleteFile(serviceName, version, fullName);

        [Get("deleteDirectory/{serviceName}/{version}")]
        public void DeleteDirectory(string serviceName, string version, string fullName) => fileService.DeleteDirectory(serviceName, version, fullName);

        [Get("createDirectory/{serviceName}/{version}")]
        public void CreateDirectory(string serviceName, string version, string path, string name) => fileService.CreateDirectory(serviceName, version, path, name);
        /// <summary>
        /// 查看配置文件夹
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <returns></returns>
        [Get("lookConfigs/{serviceName}")]
        public List<FileModel> GetConfigs(string serviceName)
        {
            return fileService.GetConfigFiles(serviceName);
        }
        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="fullName"></param>
        /// <returns></returns>
        [Get("readConfig/{serviceName}")]
        public string ReadConfig(string serviceName, string fullName) => fileService.ReadConfig(serviceName, fullName);


        /// <summary>
        /// 写入配置 
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="fullName"></param>
        /// <param name="text"></param>
        [Post("writeConfig/{serviceName}")]
        public void WriteConfig(string serviceName, string fullName, [FromBody] ContentDTO dto)
        {
            fileService.WriteConfig(serviceName, fullName, dto.Context);
            fileService.CopyConfigToRun(serviceName);
        }
        /// <summary>
        /// 删除配置
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="fullName"></param>
        [Get("deleteConfig/{serviceName}")]
        public void DeleteConfig(string serviceName, string fullName) => fileService.DeleteConfig(serviceName, fullName);

        /// <summary>
        /// 创建新版本
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        [Get]
        public string CreateVersion(string serviceName) => fileService.CreateVersion(serviceName);

        /// <summary>
        /// 获取服务的所以版本
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        [Get]
        public FileModel[] GetVersion(string serviceName) => fileService.GetVersions(serviceName);

        /// <summary>
        /// 删除版本
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="versionName"></param>
        [Get]
        public void DeleteVersion(string serviceName, string versionName) => fileService.DeleteVersion(serviceName, versionName);


        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="formFiles"></param>
        /// <returns></returns>
        [Post("upload/{serviceName}/{version}")]
        public void Upload(string serviceName, string version, string path, long t, IFormFile file)
        {
         
            fileService.Upload(serviceName, version, path, Path.GetFileName(file.FileName),t, file.OpenReadStream());
        }

        /// <summary>
        /// 上传配置
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="file"></param>
        [Post("updateConfig/{serviceName}")]
        public void UploadConfig(string serviceName, IFormFile file)
        {
            fileService.UploadConfig(serviceName, Path.GetFileName(file.FileName), file.OpenReadStream());
        }
    }
}
